﻿var m_obj=null;
var metaconsts=new SmConstClsList();

$(document).ready(function()
{
    $("#checkBGLX4").change(function(){if($("#checkBGLX4").attr("checked")=="checked"){$("#btnCKRZXX").removeAttr("disabled");}else{$("#btnCKRZXX").attr("disabled","disabled");}});

    var blhj=$.query.get("BLHJ");
    if(blhj!="SLXM"){ SetPageReadonly();$(".m_btn").addClass("nosee");}
    else{$("input.dateinput").click(function(){ WdatePicker({dateFmt:'yyyy-M-d'}); } );}
    $("#id_input_kczkz").change(function(){
		InitCLDW("id_input_kczkz","id_sel_规模单位","id_sel_CLDW");
	});
    $("input[name='txt总储量']").change(function(){
		InitCLGM("txtCLGM","id_input_kczkz","id_input_总储量","id_sel_CLDW");
	});
    $("#id_sel_CLDW").change(function(){
		InitCLGM("txtCLGM","id_input_kczkz","id_input_总储量","id_sel_CLDW");
	});
    metaconsts.Init("KCK_JJLX,KCK_KCFS,KZ@GetKZMeta,KCK_CKHDFS,KCK_JKCZFS");
    var projectid=$.query.get("PROJECT_ID");
    if(projectid!=null && projectid!='')
    {
        var dbalias="CKQDJK";   
        var tablename="采矿申请登记";
        var condittion="PROJECT_ID=\\\'"+projectid+"\\\'";
        m_obj=new SmObjectData("CKQDJK","采矿申请登记");
        m_obj.GetFirstSmObject(condittion);
        InitSQB();
    }
});

function GetKeyFromMeta(key,type,obj)
{
    var strresult="";
    if(obj!=null)
    {
        var o=null;var ii=0;
        for(ii=0;ii<obj.Data.ConstClsList.length;ii++)
        {
            if(obj.Data.ConstClsList[ii].Type==type)
            {
                o=obj.Data.ConstClsList[ii];
                break;
            }
        }
        if(o!=null)
        {
            for(ii=0;ii<o.ConstList.length;ii++)
            {
                if(o.ConstList[ii].Key==key)
                {
                    strresult=o.ConstList[ii].Value;
                    break;
                }
            }
        }
    }
    return strresult;
}
//初始化申请表
function InitSQB()
{
    var obj=m_obj;
    $("#checkBGLX0").Checked="checked";
    if(obj!=null && obj.Data.Data!=null && obj.Data.Data.length>0)
    {
        var gmdw='';
        var cldw='';
        var kslx='';
        for(i=0;i<obj.Data.Data.length;i++)
        {
            var key=obj.Data.Data[i].Key;
            var value=obj.Data.Data[i].Value;
            if(key=="经济类型")
            {
                metaconsts.FillSelectHtml($("#id_sel_经济类型"),"KCK_JJLX",value);
            }
            if(key=="开采主矿种")
            {
                $("input[name='txtKZMC']").val(GetKeyFromMeta(value,"KZ",metaconsts));
            }
            if(key=="开采方式")
            {
                metaconsts.FillSelectHtml2($("#id_sel_开采方式"),"KCK_KCFS",value);
            }
            if(key=="价款处置方式代码")
            {
                metaconsts.FillSelectHtml($("#id_sel_JKCZFSDM"),"KCK_JKCZFS",value);
            }
            if(key=="采矿权取得方式")
            {
                metaconsts.FillSelectHtml2($("#id_sel_采矿权取得方式"),"KCK_CKHDFS",value);
            }
            if(key=="变更类型")
            {
                for(j=0;j<5;j++)
                {
                    $("#checkBGLX"+j).attr("checked",value[j]==1);
                }
                if($("#checkBGLX4").attr("checked")=="checked"){$("#btnCKRZXX").removeAttr("disabled");}
            }
            if(key=="规模单位")
            {
                gmdw=value;
            }
            if(key=="储量单位")
            {
                cldw=value;
            }
            if(key=="矿石类型")
            {
                kslx=value;
            }
            if($("input[name='txt"+key+"']")!=null)
            {
               $("[name='txt"+key+"']").val(value);
            }
            
        }
        FireChangeEvent("id_input_kczkz");
        if(gmdw!='')
        {
            $("#id_sel_规模单位 option[dw='"+gmdw+"']").attr("selected",true);
        }
         if(cldw!='')
        {
            $("#id_sel_CLDW option[dw='"+cldw+"'][sm='"+kslx+"']").attr("selected",true);
        }
        if($("#txtQTKZ").val()!='')
        {
            $("#txtGBS").attr("checked",true);
            $("#txtQTKZ").removeAttr("readonly");
        }
        else
        {
            $("#txtGBS").attr("checked",false);
            $("#txtQTKZ").attr("readonly","readonly");
        }
        InitCLGM("txtCLGM","id_input_kczkz","id_input_总储量","id_sel_CLDW");
    }
}

function SetPageReadonly()
{
    $("input").attr("readonly","readonly");
    $("input").disableSelection();
    $("select").attr("disabled","disabled");
    $("textarea").attr("readonly","readonly");
    return true;
}

function selectKZ()
{
    ShowMetaTree("KZ",okSelectKZCallBack);
}
function selectXZQ()
{
    ShowMetaTree("XZQ",okSelectXZQCallBack);
}
function okSelectXZQCallBack(dm,mc)
{
    $("input[name='txt所在行政区名称']").val(mc);
    $("input[name='txt所在行政区名称']").attr("tag",dm);
}
function okSelectKZCallBack(dm,mc)
{
    document.getElementById("id_input_kczkz").value=dm;
    $("#id_input_kczkzmc").val(mc);
    FireChangeEvent("id_input_kczkz");
    FireChangeEvent("id_sel_CLDW");
}
function saveSQB()
{
    var str="法定代表人,地址,邮编,电话,矿山名称,开采主矿种,其它主矿种,设计规模,储量规模,总储量,所在行政区名称,项目审批机关,批准文号,原有效期起,原有效期止,有效期限,有效期起,有效期止,勘查许可证号,原缴纳采矿权价款,变更内容,申请原因,设计利用储量,备注";
    if(m_obj!=null)
    {
        var strs=str.split(',');
        for(i=0;i<strs.length;i++)
        {
            m_obj.SetValue(strs[i],$("[name='txt"+strs[i]+"']").val()==null?"":$("[name='txt"+strs[i]+"']").val());
        }
        m_obj.SetValue("经济类型",$("#id_sel_经济类型 option:selected").attr("value"));
        m_obj.SetValue("开采方式",$("#id_sel_开采方式 option:selected").text());
        m_obj.SetValue("采矿权取得方式",$("#id_sel_采矿权取得方式 option:selected").text());
        m_obj.SetValue("价款处置方式代码",$("#id_sel_JKCZFSDM option:selected").attr("value"));
        m_obj.SetValue("规模单位",$("#id_sel_规模单位 option:selected").attr("dw"));
        m_obj.SetValue("储量单位",$("#id_sel_CLDW option:selected").attr("dw"));
        m_obj.SetValue("矿石类型",$("#id_sel_CLDW option:selected").attr("sm"));
        var bglx="";
        for(i=0;i<5;i++)
        {
            bglx+=($("#checkBGLX"+i).attr("checked")=="checked"?'1':'0');
        }
        m_obj.SetValue("变更类型",bglx);
        alert(m_obj.Update());
    }
}
function gbsclick()
{
    if($("#txtGBS").attr("checked")=="checked")
    {
        $("#txtQTKZ").removeAttr("readonly");
    }
    else
    {
        $("#txtQTKZ").val("");
        $("#txtQTKZ").attr("readonly","readonly");
    }
}
function ChangeBGNR()
{
    var checked=$(event.srcElement).attr("checked");
    if(checked=="checked")
    {
        var text=$("#txtBGNR").text();
        if(text.indexOf($(event.srcElement).attr("tag"))<0)
        {
            $("#txtBGNR").text(text+$(event.srcElement).attr("tag"));
        }
    }
}

function PrintSQB()
{
    $("#id_div_print").printArea(); 
}